dict1 = {}
list =[]

def register():

    print("######开始注册######")
    a=input('请输入你的用户名：')
    b=input('请输入你的密码：')
    dict1[a]=b
    print("注册成功")
    menu()

def sign():
    print("【【【【输错3次将自动返回主界面】】】】")
    for i in range(3):
        username=input("请输入你的用户名：")
        passwd=input("请输入你的密码：")

        if username in dict1 and passwd==dict1[username]:
            print("登录成功")
            shop()
            break

        else:
            print("请检查用户名或者密码，或者先去注册")
            print(f"你还有{2-i}次机会")
    else:
        menu()


def menu():
    x=input("""#####欢迎使用本商城#####
    输入1开始登录
    输入2进行注册
    输入exit退出""")
    if x=='1':
        sign()
    elif x=='2':
        register()
    elif x=='exit':
        exit(0)
    else:
        print("请按指示输入")
        menu()



def shop():
    money = float(input("请输入你的预算并扫码支付："))
    print("请开始选购")
    dict2 = {}
    num3=0
    i=0
    furit = {"F01": {'name': '苹果', 'price': '1.2'}, 'F02': {'name': '香蕉', 'price': '5.5'},
             'F03': {'name': '菠萝', 'price': '3.0'}, \
             "F04": {'name': '西瓜', 'price': '4.5'}, 'F05': {'name': '橘子', 'price': '4.0'},
             'F06': {'name': '桃子', 'price': '6.5'}, \
             "F07": {'name': '牛肉', 'price': '45.0'}, 'F08': {'name': '猪肉', 'price': '30.0'},
             'F09': {'name': '鸡蛋', 'price': '1.0'}}
    for key, value in furit.items():
        print(key, ":", value)

    while True:
        num1 = input('请输入你要购买的商品编号或者输入exit退出:')
        if num1=='exit':
            with open('购物清单.txt', 'r+', encoding='utf-8') as f2:
                str = f2.read()
                print("购物清单：\n", str)
                print("共消费:", num3, "元")
                f2.truncate(0)
            break
        else:
            num2=input('请输入你要购买的数量：')
            num =float(furit[num1]['price'])*int(num2)
            dict2['商品']=furit[num1]['name']
            dict2['数量']=num2
            print(f"本次购物情况{dict2}")

            if num < money:
                print(f"本次购物过后你的余额为{money-num}")
                money=money-num
                num3 += num
                with open('购物清单.txt', 'a+', encoding='utf-8') as f:
                    f.write(f"{dict2}\n")
            else:
                print(money)
                print("你的余额已不足，请尽快充值")
                x = input("#####按1进行充值或者任意输入打印购物清单#####")
                if x == '1':
                   y = float(input("请输入你要充值的金额"))
                   money=money+y
                else:
                    with open('购物清单.txt', 'r+', encoding='utf-8') as f2:
                        str = f2.read()
                        print("购物清单：\n", str)
                        print("共消费:", num3, "元")
                        print("余额为",money,"元")
                        f2.truncate(0)

menu()

